package org.jackysoft.interceptor;

import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;
import org.jackysoft.util.ReflectionUtil;

import com.opensymphony.xwork2.ActionInvocation;

public class PagerInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3533898193461226453L;
	@Override
	public String intercept(ActionInvocation invokor) throws Exception {
		super.intercept(invokor);
		Object action = invokor.getAction();
		Integer page = (Integer) parseBean(action,"page");
		Integer rows = (Integer) parseBean(action,"rows");
		if(page==null)page=0;
		if(rows==null)rows=10;
		// 获得这个action中所对应的class类型参数
		Method getsPage_Method = service.getClass().getMethod("getsPage",Integer.class,Integer.class);
		// 调用service的插入bean方法
		
		Object pageBean = getsPage_Method.invoke(service, page,rows);
		PropertyUtils.setProperty(pageBean,"actionClass", action.getClass());
		PropertyUtils.setProperty(pageBean, "method", invokor.getProxy().getMethod());
		PropertyUtils.setProperty(action, "pager", pageBean);		
		return invokor.invoke();
	}
}
